// PartCrudeDataSet.cpp: implementation of the PartCrudeDataSet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PartCrudeDataSet.h"

#include "DBConnectionManager.h"
#include "ProductCatalogManager.h"
#include "ProductCatalog.h"
#include "PartFormat.h"
#include "PersistentFacade.h"
#include "StandardGenerator.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace JointSystem;
using namespace DAWrapper;

PartCrudeDataSet::PartCrudeDataSet()
{

}

PartCrudeDataSet::~PartCrudeDataSet()
{

}

CString PartCrudeDataSet::GetCustomIDField(const CString& strPartID)
{
	ASSERT(m_theTables.GetSize() != 0);
	CString strTemp;

	for(int i = 0; i < m_theTables.GetSize(); i++)
	{
		if(!strTemp.IsEmpty())
		{
			strTemp += _T(" and ");
		}
		strTemp += m_theTables.GetAt(i) + _T(".CustomID = '") + strPartID + _T("'");
	}
	return strTemp;
}

BOOL PartCrudeDataSet::InstancePart(Part* pPart, const CString& strBH)
{
/*	ASSERT(pPart);
	ASSERT(!strBH.IsEmpty());
	ASSERT(!pPart->GetCustomID().IsEmpty());

	m_theTables.RemoveAll();

	pPart->SetBH(strBH);

	CString strSql;
	CString strTable = GetDataTableName(pPart->GetCustomID());
	strSql.Format(_T("select *, %s.CustomID as CustomID from %s where BH = '%s' and %s"),
		m_theTables.GetAt(0), strTable, strBH, GetCustomIDField(pPart->GetCustomID()));

	return GetPartCrudeData(pPart, strSql);*/
	return TRUE;
}

BOOL PartCrudeDataSet::InstancePart(Part* pPart, const CString& strID, const CString& strTerm)
{
	ASSERT(pPart);
	ASSERT(!strID.IsEmpty());
	ASSERT(!strTerm.IsEmpty());

	m_theTables.RemoveAll();

	CString strSql;
	CString strTable = GetDataTableName(pPart->GetCustomID());
	strSql.Format(_T("select *, %s.CustomID as CustomID from %s where %s and %s"),
		m_theTables.GetAt(0), strTable, GetCustomIDField(strID), strTerm);
	
	return GetPartCrudeData(pPart, strSql);
}

CString PartCrudeDataSet::GetDataTableName(const CString& strID)
{
	ASSERT(m_theTables.GetSize() == 0);
	ProductCatalog* pCatalog = ProductCatalogManager::Instance()->GetProductCatalog(this->GetDataStandard());
	Standard* pStandard = GetPersistentFacade().GetStandardGenerator()->Generate(pCatalog);
	ASSERT(pStandard);

	ASSERT(!pStandard->GetCrudeDataTable().IsEmpty());
	ASSERT(!pStandard->GetIDDataTable().IsEmpty());
	this->m_theTables.Add(pStandard->GetCrudeDataTable());
	this->m_theTables.Add(pStandard->GetIDDataTable());
	return pStandard->GetCrudeDataTable() + _T(",") + pStandard->GetIDDataTable();
}

BOOL PartCrudeDataSet::GetPartCrudeData(Part* pPart, const CString& strSql)
{
	BOOL bResult = FALSE;
	ASSERT(!strSql.IsEmpty());
	try
	{
		ASSERT(m_rs == NULL);
		CString strDataSource;
		strDataSource = JointSystem::ProductCatalogManager::Instance()->
			GetProductCatalog(this->GetDataStandard())->DataSourceURI();
		ASSERT(!strDataSource.IsEmpty());
		_ConnectionPtr pCon = *DBConnectionManager::Instance()->GetConnectionPtrByUri(strDataSource);
		m_rs = pCon->Execute(_bstr_t(strSql), NULL, adCmdText);
		ASSERT(m_rs != NULL);
		if(!m_rs->adoEOF && !m_rs->BOF)
		{
			pPart->CloseGetPropertyTrigger();
			bResult = this->InitProperty(*pPart);
			//			GetCurDatabase().ReadPartCrudeData(*pPart, m_rs);
			pPart->OpenGetPropertyTrigger();
		}
		else{
			m_rs->Close();
			return FALSE;
		}
	}
	CATCH_COM_ERROR(m_rs, FALSE)

	return bResult;
}